<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class ModifyUser extends HTML_QuickForm {
  private $tpl;
  private $user;
  private $login;

  function ModifyUser($tpl,$user,$login) {
    $this->tpl = $tpl;
    $this->user = $user;
    $this->login = $login;

    $data = $user->getAll($login);

    $roles = $user->getRoles();
    $this->tpl->assign('roles', $roles);

    $defaults = array('firstname' => $user->getFirstname(),
                      'lastname' => $user->getLastname(),
                      'email' => $user->getEmail(),
                      'login' => $user->getLogin(),
                      'sex' => $user->getSex(),
                      'status' => $user->getStatus(),
                      'ahosts' => str_replace('|', "\r\n",$user->getAhosts()));

    if ($user->getUser_roles() != "") {
      foreach ($user->getUser_roles() as $value) {
        $defaults[$value['rid']] = true;
      }
    }

    $this->setDefaults($defaults);

    $this->setConstants(array('password1' => 'no change',
                              'password2' => 'no change'));


    parent::HTML_QuickForm('modifyuser','post','','','onsubmit="javascript:password1.value=hex_sha1(password1.value), password2.value=hex_sha1(password2.value)"');
    $this->addElement('header', 'header', 'adduser');
    $this->addElement('text', 'firstname', _('firstname'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('text', 'lastname', _('lastname'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('text', 'email', _('email'), array('size' => 30, 'maxlength' => 100));
    $this->addElement('static', 'login', _('login'));
    $this->addElement('password', 'password1', _('password'), array('size' => 30, 'maxlength' => 50));
    $this->addElement('password', 'password2', _('repeat password'), array('size' => 30, 'maxlength' => 50));
    $sex[] =& $this->createElement('radio', null, null, _('male'), 'M', array('class' => 'radio'));
    $sex[] =& $this->createElement('radio', null, null, _('female'), 'F', array('class' => 'radio'));
    $this->addGroup($sex, 'sex', _('sex'), '&nbsp;');
    if($user->getUid() != 1) {
      if (!empty($roles)) {
        foreach ($roles as $value) {
          $checkbox[] =& $this->addElement('checkbox', $value['rid'], $value['name']);
        }
        $this->addGroup($checkbox, 'checkbox', null);
      }
      $status[] =& $this->createElement('radio', null, null, _('active'), '1', array('class' => 'radio'));
      $status[] =& $this->createElement('radio', null, null, _('inactive'), '0', array('class' => 'radio'));
      $this->addGroup($status, 'status', _('status'), '&nbsp;');
      $this->addElement('textarea', 'ahosts', _('allowed hosts'), array('wrap' => 'off', 'rows' => '2', 'cols' => '23'));

    }
    $this->addElement('submit', 'btn_Submit', _('submit'), array('class' => 'submit', 'onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#728FA4\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('compare', 'function', 'comparePassword', $this);
    $this->registerRule('check_exists_email','function','checkExistsEmail', $this);
    $this->registerRule('check_exists_login','function','checkExistsLogin', $this);
    $this->addRule('email', _('email required'), 'required');
    $this->addRule('email', _('verify your email'), 'email');
    $this->addRule('email', _('email already exists'), 'check_exists_email', $this->user);
    $this->addRule('password1', _('password required'), 'required');
    $this->addRule('password2', _('password required'), 'required');
    $this->addRule('password1', _('min lenght 4'), 'minlength', '4');
    $this->addRule('password2', _('min lenght 4'), 'minlength', '4');
    $this->addRule('password1', _('passwords do not match'), 'compare');
    $this->addRule('sex', _('sex required'), 'required');
    $this->addRule('ahosts', _('characters not allowed'), 'regex', '/^[a-zA-Z0-9\.-:\r\n]+$/');
  }

  function comparePassword() {
    if ($this->getSubmitValue('password1') == $this->getSubmitValue('password2')) {
       return true;
    } else {
       return false;
    }
  }

  function checkExistsEmail() {
    $this->user->setEmail($this->getSubmitValue('email'));
    return $this->user->checkExists($op='modify');
  }


  function displayForm() {
    $this->renderer =& new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->user->getMsg());
  }

  function process() {
    $this->user->ModifyUser($this->getSubmitValues());
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->user->getMsg());
  }

} // end of class

?>